const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    eIndex: 0,
       pageSize: 5,
       pageNumber: 1,
       orderStatus: 0
  },

  // 菜单切换类名on
  tab: function (e) {
    this.setData({
      eIndex: e.currentTarget.dataset.index,
      orderStatus: e.currentTarget.dataset.index
    })
     this.setData({
          pageNumber: 1
     });
     this.loadOrder()

  },

  // 跳转到订单详情
  toOrderDetails: function (e) {
     let id = e.currentTarget.id
    wx.navigateTo({
      url: '../order-details/order-details?orderId='+id
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
       let orderStatus = options.orderStatus
       this.setData({
            orderStatus: orderStatus,
            eIndex: orderStatus
       });
       this.loadOrder()
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
       let pageNumber = this.data.pageNumber
       pageNumber = pageNumber + 1
       this.setData({
            pageNumber: pageNumber
       });
       let that = this
       var data = {
            "memberId": app.globalData.userInfo.id,
            "pageSize": that.data.pageSize,
            'pageNumber': that.data.pageNumber,
            "orderStatuses": that.data.orderStatus
       }
       var promise = app.synPostData('order/select', data)
       promise.then(function (res) {
            let orders = that.data.orders.concat(res.data.result)
            that.setData({
                 orders: orders
            });
       });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (e) {
       

       if (e.target.dataset.value.cartType == 3) {
            return {
                 title: 'JNBYHOME',
                 path: '/pages/open-gift/open-gift?orderId=' + e.target.dataset.value.id,
                 imageUrl: '/img/share.jpg'
            }
       }
  },
  loadOrder:function(){
       let that = this
       var data = {
            "memberId": app.globalData.userInfo.id,
            "pageSize": that.data.pageSize,
            'pageNumber': that.data.pageNumber,
            "orderStatuses": that.data.orderStatus
       }
       var promise = app.synPostData('order/select', data)
       promise.then(function (res) {
            that.setData({
                 orders: res.data.result
            })
       });
  },
    receiveOrder:function(e){
         let orderId = e.currentTarget.id
         let that=this
         wx.showModal({
              title: '提示',
              content: '是否已收货',
              success: function (res) {
                   if (res.confirm) {

                        wx.request({
                             url: app.globalData.baseURL + '/order/receiveOrder',
                             method: "POST",
                             header: {
                                  "Content-Type": "application/x-www-form-urlencoded"
                             },
                             data: {
                                  "orderId": orderId
                             },
                             success: function (res) {
                                  that.loadOrder()
                             }
                        })

                   } else if (res.cancel) {

                   }
              }
         })
  },
     toPayOrder: function (e){
          let orderId = e.currentTarget.dataset.value.id
          let payAmount = e.currentTarget.dataset.value.payAmount
          let cartType = e.currentTarget.dataset.value.cartType
          let that = this
          wx.navigateTo({
               url: '../payment/payment?orderIds=' + orderId + '&payAmount=' + payAmount + '&cartType=' + cartType
          })

     }

})